package com.bw.zg3park2212.news

import android.app.DatePickerDialog
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bw.base.entity.NewsAddParam
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.R
import com.bw.zg3park2212.databinding.ActivityNewsAddBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import java.util.Calendar

@AndroidEntryPoint
@Route(path = "/app/NewsAddActivity")
class NewsAddActivity : BaseActivity<ActivityNewsAddBinding,NewsViewModel>() {
    var iconurl=""
    var types=""
    override fun intitView() {
        binding.newsIcon.setOnClickListener {
            PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }
        binding.tvData.setOnClickListener {
            var calendar=Calendar.getInstance()
            DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
                var mm= if(month<10) "0"+(month+1) else month+1
                var dd= if(dayOfMonth<10) "0$dayOfMonth" else dayOfMonth
                binding.newsTime.setText("$year-$mm-$dd")
            },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show()
        }
        var typelist= mutableListOf<String>("世界新闻","园区新闻")
        val typeAdpter=ArrayAdapter(this,android.R.layout.simple_spinner_item,typelist)
        typeAdpter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.newsType.adapter=typeAdpter
        binding.newsType.onItemSelectedListener=object : AdapterView.OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long,
            ) {
                types=typelist[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

        }
        binding.check.setOnClickListener {
            val newsAddParam=NewsAddParam()
            newsAddParam.content=binding.newsContent.text.toString()
            newsAddParam.title=binding.newsTitle.text.toString()
            newsAddParam.type=types
            newsAddParam.publisher=binding.newsName.text.toString()
            newsAddParam.publishDate=binding.newsTime.text.toString()
            newsAddParam.images= mutableListOf(iconurl)
            viewmodel.newsadd(newsAddParam)
        }
    }

    override fun intitData() {
        viewmodel.newsaddSuucse.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==PictureSelector.SELECT_REQUEST_CODE&&resultCode==RESULT_OK){
            val bean = data!!.extras!!.get(PictureSelector.PICTURE_RESULT) as PictureBean
            Glide.with(this).load(bean.path).into(binding.newsIcon)
            iconurl=bean.path
        }
    }
}